define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        utils = require('js/utils/tools'),
        models = require('js/apps/op/models'),
        helper = require('js/apps/report/common'),
        app = Backbone.history;

    var WithdrawBlackView = Backbone.View.extend({
        events: {
            'click #exportBtn': 'doExport',
            'click #search': 'doSearch',
        },
        initialize: function (options) {
            this.options = options;
            this.collection = new models.WithdrawBlackCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        doExport: function () {
            notify.warning('警告', '导出中，耗时较长请等待...');
            var that = this;
            var type = this.options.type;
            var status = this.options.status;
            $.get('/admin/withdraw/black/get/?$export=1&type='+type+'&status='+status).done(function (resp) {
                window.location = resp.data.url;
            }).fail(function (data) {
                notify.notifyResp(data);
            });
        },
        _getSearchOption: function () {
            var options = {};
            var searched = utils.getAllInput('#sidebar_right');
            searched.type = $('#type').val();
            searched.account_id = $('#searchUid').val();
            if (searched.account_id) {
              options.user_id = searched.account_id;
            }
            searched.status = $("#status").val();
            if(null != searched.type && "all" != searched.type){
                options.type = searched.type;
            }
            if(null != searched.status && 0 != searched.status){
                options.status = searched.status;
            }
            utils.saveSearched('op/withdraw_black/', searched);
            return options;
        },
        doSearch: function () {
            var options = this._getSearchOption();
            options.$page = 1;
            options.$size = 20;
            options._ = new Date().getTime();
            app.navigate(utils.composeQueryString('#op/withdraw_black/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                searched: utils.getSearched('op/withdraw_black/')
            }));

            var that = this;

            $('#addNewItem').magnificPopup({
                items: {
                    src: '#addNewItemPanel',
                    type: 'inline'
                },
                midClick: true,
                closeOnBgClick: false,
                callbacks: {
                    beforeOpen: function () {
                        that.uuid = utils.generateUUID();
                        $('#cancelAddNewItem').off('click').click(function () {
                            $.magnificPopup.close();
                        });

                        $('#confirmAddNewItem').off('click').click(function () {
                            var type = $('#inputStatus').val();
                            var type_account = $('#type_account').val();
                            var note = $('#note').val();
                            $.post('admin/withdraw/black/set/', JSON.stringify({
                                'type': type,
                                'type_account': type_account,
                                'note': note
                            })).done(function (resp) {
                                $.magnificPopup.close();
                                Backbone.history.loadUrl();
                                notify.success('提示', '保存成功')
                            }).fail(function (data) {
                                notify.notifyResp(data)
                            })
                        });
                    }
                }
            });

            $('.modify-item').magnificPopup({
                items: {
                    src: '#editWithdrawPanel',
                    type: 'inline'
                },
                midClick: true,
                closeOnBgClick: false,
                callbacks: {
                    beforeOpen: function () {
                        $('#riskInfo').html('');
                        var data_id = $(this.st.el).closest('tr').data('id'),
                            choosen = that.collection.get(data_id);
                        var type = choosen.get('type');
                        var type_account = choosen.get('type_account');
                        var status = choosen.get('status');

                        $.get('/admin/withdraw/black_modification/get/?mod_type='+type+'&type_account='+type_account).done(function (resp) {
                            var data = resp.data.list;
                            document.getElementById('op_infos').innerHTML="";
                            var op_infos = document.getElementById('op_infos');
                            for(var i=0;i<data.length;i++){
                                var op_info = document.createElement("p");
                                var node=document.createTextNode("操作时间："+data[i].created_at+"   操作选项："+WITHDRAW_BLACK_STATUS[data[i].status]
                                    +"  操作人ID："+data[i].admin_id+"  备注信息："+ (data[i].reason || '无'));
                                op_info.appendChild(node);
                                op_infos.appendChild(op_info);
                            }
                        }).fail(function (data) {
                            notify.notifyResp(data);
                        });

                        if(1 == status){
                            $('#submitLock').hide();
                            $('#submitUnlock').show();
                            status = 2;
                        }else if(2 == status){
                            $('#submitUnlock').hide();
                            $('#submitLock').show();
                            status = 1;
                        }
                        $('#submitLock').off('click').click(function() {
                            withdraw_black.setWithdrawBlackStatus(type_account, type, status);
                            $.magnificPopup.close();
                        });
                        $('#submitUnlock').off('click').click(function() {
                            withdraw_black.setWithdrawBlackStatus(type_account, type, status);
                            $.magnificPopup.close();
                        });
                        $('#submitClose').off('click').click(function() {
                            $.magnificPopup.close();
                        });

                    },
                }
            });
            utils.getPaginator(that.options, this.collection.total, '#op/withdraw_black/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/WithdrawBlack.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            this.options.$size = 20;
            var view = new WithdrawBlackView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});


withdraw_black = {
    setWithdrawBlackStatus : function(type_account, type, status){
        var reason = window.prompt("请填写操作原因");
        if (!reason) {
            alert("操作原因不能为空");
        }else{
            $.post('admin/withdraw/black/set/', JSON.stringify({
                'type': type,
                'type_account': type_account,
                'status': status,
                'reason': reason
            })).done(function (resp) {
                location.reload();
                alert('操作成功！');
            }).fail(function (data) {
                alert('操作失败！');
            })
        }
    }
}